<?php
require_once '_inc.php';

$op = $_GET['op'];
$file = $_GET['file'];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$file = $_POST['editfile'];
	$content = !empty($_POST['content']) ? trim($_POST['content']) : '';
	if ( !empty($content) )
	{
		$content = stripslashes($content);
		$filename = explode('/', $file);
		copy($file, '../templates/bak.'.$filename[2]);
		$done = util::writefile($file, $content);
		if ( $done ) { 
			$msg = "修改成功";
		} else {
			$msg = "修改失败";
		}
		$tpl->assign('msg', $msg);
		$tpl->display('_msg.tpl');
		exit();
	}
}

switch($op)
{
	case 'edit':
		if(file_exists($file))
		{
			$content = util::readfile($file);
			$tpl->assign('file',$file);
			$tpl->assign('content', htmlspecialchars($content));
			$tpl->display('tpl_edit.tpl');
		}
		break;
		
	case 'del':
		if(file_exists($file))
		{
			@unlink($file);
			header('location: tpl_list.php');
			exit();
		}
		break;
}